package com.chuanyang.driverh.mainhome;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainhome.adapter.FleetSearchAdapter;
import com.chuanyang.driverh.response2.join.ResultSearchList;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class SearchDriverActivity extends BaseTitleActivity {

    private ListView lv_list;
    private EditText et_search;
    private ImageView iv_clear_text;

    private FleetSearchAdapter fleetSearchAdapter;
    private List<ResultSearchList.DataBeanX.DataBean> searchDatas = new ArrayList<>();

    private int page = 1;

    @Override
    protected int getContentResId() {
        return R.layout.activity_search_driver;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setBackImg();
        getTitleTextView().setText("搜索");
        lv_list = (ListView) findViewById(R.id.lv_list);
        et_search = (EditText) findViewById(R.id.et_search);
        et_search.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        et_search.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        et_search.setSingleLine(true);
        iv_clear_text = (ImageView) findViewById(R.id.iv_clear_text);
        iv_clear_text.setOnClickListener(this);

    }

    @Override
    protected void initData() {

        et_search.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(SearchDriverActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    if(et_search.getText().toString().trim() != null){
                        search(et_search.getText().toString().trim());
                    }else {
                        Toast.makeText(SearchDriverActivity.this,"请输入要搜索的内容",Toast.LENGTH_SHORT).show();
                    }
                }
                return false;
            }
        });

        fleetSearchAdapter = new FleetSearchAdapter(SearchDriverActivity.this);
        lv_list.setAdapter(fleetSearchAdapter);
        //检测lv_list滑动到底部
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.iv_clear_text:
                et_search.setText("");
                break;
        }
    }
    @Override
    protected void initListener() {

    }

    private void search(String search){
        DialogUtil.showLoadingDialog(SearchDriverActivity.this,"加载中...");
        Map<String,Object> paramssss =new HashMap<String, Object>();
        paramssss.put("type", "2");//1：司机 2：车队长
        paramssss.put("fleetId", SharedPrefUtil.getFleetId());
        paramssss.put("search",search);
        paramssss.put("page",page);
        paramssss.put("length","30");

        MyHttpUtil.addBusinessRelation(SearchDriverActivity.this,paramssss, new MyCallback<ResultSearchList>(
                SearchDriverActivity.this,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {

            }
            @Override
            public void onSuccess(ResultSearchList result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        if(result.getData().getData().size() > 0){
                            fleetSearchAdapter.setNewData(result.getData().getData());
                        }
                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(SearchDriverActivity.this,result.getMsg());
                }
            }
        });
    }
}
